package com.cskaoyan.util;

import com.cskaoyan.exception.MarketValidationException;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * @author LC_RSH
 * @date 2022/6/29 21:20
 */
public class ValidationUtils {

    public static void valid(BindingResult bindingResult) throws MarketValidationException {
        if (bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            // 请求参数名
            String field = fieldError.getField();
            // 错误的值
            Object rejectedValue = fieldError.getRejectedValue();
            // 错误的信息
            String defaultMessage = fieldError.getDefaultMessage();
            String msg = "请求参数" + field + "没有通过校验:值为" + rejectedValue + ";" + defaultMessage;
            throw new MarketValidationException(msg);
        }
    }
}

/**
 * 参数正则验证
 *
 * @author 86157
 * @since 2022/06/29 21:20
 */